package com.aruba.attendance.home.view

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PhotoCamera
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.aruba.attendance.R
import com.aruba.attendance.ui.widget.calendar.CalendarView
import com.aruba.attendance.ui.widget.calendar.SelectDateState
import com.google.accompanist.pager.ExperimentalPagerApi

/**
 * 主界面
 * Created by aruba on 2023/2/13.
 */
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPagerApi::class)
@Composable
fun HomeScaffold(
    toSetting: (() -> Unit)? = null,
    toStartPhoto: (() -> Unit)? = null,
    initialSelectDate: SelectDateState? = null,//选中的日期与跳转状态
    onDateSelected: ((year: Int, month: Int, dayOfMonth: Int) -> Unit),
    snackbarState: SnackbarHostState = remember { mutableStateOf(SnackbarHostState()) }.value,
    content: @Composable (BoxScope.() -> Unit)? = null,
    calendarContent: @Composable (BoxScope.(scrollState: LazyListState) -> Unit)? = null
) {
    Scaffold(
        topBar = {
            TopAppBar(
                modifier = Modifier.height(40.dp),
                title = {//标题
                    Text(
                        modifier = Modifier.padding(start = 10.dp),
                        text = " ",
                        color = MaterialTheme.colorScheme.onSurface
                    )
                },
                actions = {//右侧按钮
                    IconButton(onClick = { toSetting?.invoke() }) {
                        Icon(
                            Icons.Filled.Settings,
                            tint = MaterialTheme.colorScheme.onSurface,
                            contentDescription = stringResource(R.string.setting)
                        )
                    }
                },
                colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = MaterialTheme.colorScheme.surface)
            )
        },
        floatingActionButton = {
            FloatingActionButton(onClick = {
                toStartPhoto?.invoke()
            }) {
                Icon(
                    Icons.Default.PhotoCamera,
                    contentDescription = stringResource(R.string.punch_the_clock)
                )
            }
        },
        snackbarHost = { SnackbarHost(hostState = snackbarState) },
    ) { paddings ->
        Box(
            modifier = Modifier.padding(paddings)
        ) {
            CalendarView(
                modifier = Modifier
                    .fillMaxHeight()
                    .padding(start = 10.dp, end = 10.dp),
                fixedHeight = true,
                initialSelectDate = initialSelectDate,
                onDateSelected = onDateSelected
            ) {
                val state = rememberLazyListState()

                Box(modifier = Modifier.animateContentSize()) {
                    calendarContent?.invoke(this, state)
                }

//                ExtendedFloatingActionButton(
//                    text = { Text(text = stringResource(R.string.punch_the_clock)) },
//                    icon = {
//                        Icon(
//                            Icons.Default.PhotoCamera,
//                            contentDescription = stringResource(R.string.punch_the_clock)
//                        )
//                    },
//                    onClick = { toStartPhoto?.invoke() },
//                    expanded = !state.isScrollInProgress,
//                    modifier = Modifier
//                        .align(Alignment.BottomCenter)
//                        .padding(bottom = 10.dp)
//                )
            }

            content?.invoke(this)
        }
    }
}
